<?
/*
header ("Content-Type: text/xml");

$official = implode('', file('http://www.madwolfsw.com/dirrss.xml') );

$official = str_replace("xmlns:mw","xmlns:cq",$official);
$official = str_replace("mw:population","cq:count",$official);

print $official;
exit;
*/


$pathdata= file_to_array("path.config");
$path    = trim($pathdata[0]);
$dirname = trim($pathdata[1]);
$fi      = $_GET["f"];
$dirname=".";
$dh      = opendir( $dirname ) or die ("couldn't open directory"); 
$real    = realpath($dirname); 
$relpath = str_replace("\\","/",$real); 
$relpath = str_replace($path,"",$relpath);

if (count($_FILES)>0) {
	foreach( $_FILES as $file_name => $file_array ) {
		/*
		print "path: ".$file_array['tmp_name']."<br>\n";
		print "name: ".$file_array['name']."<br>\n";
		print "type: ".$file_array['type']."<br>\n";
		print "size: ".$file_array['size']."<br>\n";
	*/
		$upload_path = $file_array['tmp_name'];
		$upload_name = $file_array['name'];
		$upload_type = $file_array['type'];
		$upload_size = $file_array['size'];
		
		$filetype = exif_imagetype($upload_path); 
		if (strlen($filetype)==0) {
		    print "<font color=red>".$upload_name." does not appear to be an image. Please upload a GIF, JPG or PNG image.</font>";
		} else if ($filetype<4) {
			if ( is_uploaded_file( $upload_path )  ) {
				$tmp = $pathdata[1]."/".$upload_name;
  
				copy($upload_path, $tmp) or die("unable to create info");	
				  
				$editcookie = $_COOKIE["editcookie"]; 
				$editconfig = "_PIMP_edit_data_".$editcookie.".log";
				if (strlen($editcookie)>0) { 
					$arrconfig  = file_to_array($editconfig); 
					$fcontents  = "";
					$unique = true;
					
					//$real    = realpath($pathdata[1]);  
					$relpath = str_replace("\\","/",trim($pathdata[1])); 
					$relpath = str_replace(trim($pathdata[0]),"",$relpath);
					 
					$filename = $relpath."/".$upload_name; 
					
					// rebuld file
					for ($e=1;$e<count($arrconfig);$e++) {
						 parse_str($arrconfig[$e]);
						 if ($file==$tmp)  {
							 $unique=false; 
						 }
						 $fcontents.=$arrconfig[$e]."\n"; 
					}  
					
					$fcontents = "selected=".$tmp."\n".$fcontents;
					
					// add new file to list
					if (($remove=="")&&$unique) {
						$oi = @imagecreatefromjpeg($tmp); 
						list($lw,$lh) = getimagesize($tmp);   
						$fcontents.="file=".$tmp."&name=".$filename."&size=".$lw.",".$lh."&zoom=1\n";
					} 
					
					
					print "<xmp>".$fcontents."</xmp>"; 
					WriteToFile($editconfig, $fcontents);
					
				}
				 
				 print "<script language=jscript>location.href='fmgr.php?f=".$fi."'</script>"; 
			}  
		} else {
		    print "<font color=red>".$upload_name." cannot be uploaded. Please upload a GIF, JPG or PNG image.</font>";
		}
		exit; 
		 
	} 
}

/*

				if (file_exists($tmp)) {
				?>
				<font size="3" face="Verdana, Arial, Helvetica, sans-serif">File already exists</font>
				<br>
				<img src="thumbnail.php?i=<?=$tmp?>&w=132"><br>
				<?= $upload_name ?> already exists. Would you like to add this file?
				<?
				exit;
				} else {
					 
				}  

*/


?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PIMP Open Files</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<script language="javascript">
// function fb_edit -----------------------------------------------------'
function fb_edit() {
// ----------------------------------------------------------------------'   
    var n,q,s,b,h,a,f=fb_edit.arguments;
    h = location.href.split("?"); 
    if (h[1]) {      
        a = h[1];
        for (var k=0;k<f.length;k+=2) {
			s = "";
			b = false;
            q = a.split("&");
			for (var i=0;i<q.length;i++) {
			    n = q[i].split("=");
			    if (n[0]==f[k]) {
			        s += (s==""?"":"&")+f[k]+"="+f[k+1];
			        b = true;
			    } else s += (s==""?"":"&")+q[i];
			}
			
			if (!b) s += (s==""?"":"&")+f[k]+"="+f[k+1];
			a = s; 
        }                
    } else { 
        s = "";
        for (var k=0;k<f.length;k+=2)
            s += (s==""?"":"&")+f[k]+"="+f[k+1];  
    } 
    try {
         s += "&top="+(pageform.html.scrollTop)
    } catch (ex) {    }
    location.href = "index.php?"+s;
} // fb_edit
// **********************************************************************'
function openDir(i) {
   var d=i.substr(0,i.lastIndexOf("/"));
   fb_edit('e','b','d',d);
}
</script>
<body>
<a href="index.php"><img src="images/logo.jpeg"></a><font size=3 face="Verdana, Arial, Helvetica, sans-serif">
Gallery Page
</font><hr>
<?

 
while (! ( ( $file = readdir ( $dh ) ) == false ) ) { 	 
    if (! ($file == ".") ) {
		$subfolder = $relpath."/".$file; 
		$abspath   = $real."/".$file;
		$abspath   = str_replace("\\","/",$abspath); 
		if ( $file == ".." ) { 
		    if ( realpath($abspath) == realpath ($path)) {
			
			} else {
			    $folders [count($folders)] = $abspath;
				$parent = $abspath;
			}  
		} else if (isreadable ($abspath)) {
		     if ($fi=="") print "<a style='font:8pt tahoma;color:blue' href='fmgr.php?f=".$file."'>".$file."</a><br>";
			 if ($fi==$file) {
			    $arr = file_to_array($file); 
			    print "<table cellpadding=1 cellspacing=1 ><tr>";
				 parse_str($arr[0]);
				for ($e=1;$e<count($arr);$e++) {
				    parse_str($arr[$e]);
					$clr =$file==$selected?"CC3399":"9187CE";
				    print "<td bgcolor=$clr valign=top><img src='index.php?i=pv' align=left>";
					print "<a title='edit this image' href='index.php?r=on&e=e&efile=".str_replace($path,"",$file)."'";
					print " style='font:7pt tahoma;color:white'>".filetitle($name)."</a>";
					print "<div style='font:7pt tahoma;color:yellow'>Size: ".str_replace(","," x ",$size)."</div>";
					print "<div align=center>";
					print "<a href='index.php?r=on&e=e&efile=".str_replace($path,"",$file)."'>";
					print "<img alt='edit this image'";
					print "border=0";
					print " src='thumbnail.php?fo=100&bg=$clr&ft=jpeg&w=132&i=".$file."'>";
					print "</a></div>";
					print "<img src='index.php?i=pu' align=absmiddle>";
					print "<a href='thumbnail.php?fo=100&bg=$clr&ft=png&w=132&i=".$file."&ff=".filetitle($name).".png'";
					print " style='font:7pt tahoma;color:white'>Download avatar PNG</a><br>";
					print "<img src='index.php?i=m' align=absmiddle>";
					print "<a href='javascript:openDir(\"".$file."\")'";
					print " style='font:7pt tahoma;color:white'>Open Folder</a><br>";
				    print "</td>";
					if (($e)%6==0) print "</tr><tr>";
				}
			    print "</tr></table>";
			 }
		}
    } 
}
closedir( $dh ); 


function filetitle($thepath) { // filetitle
    $v = split ("/",$thepath);
	$v = $v[count($v)-1];
//	$v = count($v)>10?"..."+$v.substr($v.length-10):$v;
	return $v;
} // filetitle

function file_to_array($path) { 
   if (file_exists($path)) {
	$fcontents  = implode ('', file ($path)); 
	$fcontents  = trim ($fcontents);
	return explode("\n", $fcontents);
   } else return array();
}
function isreadable ($path) {
    
	 
    return strpos($path,"PIMP_edit_data")>0;
}

function WriteToFile($path, $content) {
  if(file_exists($path))  unlink ($path);
	$fp = fopen($path, 'w');
	fwrite($fp, $content);
	fclose($fp);  
}
?>

					<font size=2 face="Verdana, Arial, Helvetica, sans-serif"><b>Upload a File to Edit</b></font>
					<form method="post" action="fmgr.php?f=<?= $fi ?>" enctype="multipart/form-data">
					
					
					<input size="15" type="file" name="uploadfile"> 
					 <input type="hidden" name="MAX_FILE_SIZE" value="151200">
					<input type=submit value="Upload">
					 
					</form>
</body>
</html>
